<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button>按钮</button>

  <script>
    function addEventListener(elem, type, listener) {
      if (elem.addEventListener) {
        elem.addEventListener(type, listener)
      } else if (elem.attachEvent) {
        elem.attachEvent('on' + type, listener)
      } else {
        elem['on' + type] = listener
      }
    }

    function removeEventListener(elem, type, listener) {
      if (elem.removeEventListener) {
        elem.removeEventListener(type, listener)
      } else if (elem.detachEvent) {
        elem.detachEvent('on' + type, listener)
      } else {
        elem['on' + type] = null
      }
    }

    const elem = document.querySelector('button')

    function onClick() {
      console.log('onClick')
    }

    // 注册事件监听
    addEventListener(elem, 'click', onClick)

    // 注销事件监听
    removeEventListener(elem, 'click', onClick)
  </script>
</body>

</html>